package com.cy.core;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WebServer {

    private ServerSocket serverSocket;

    private ExecutorService threadPool;

    public WebServer() {
        try {
            System.out.println("正在启动服务器");
            this.serverSocket = new ServerSocket(8081);
            threadPool = Executors.newFixedThreadPool(5);
            System.out.println("启动服务器完毕");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        try {
            while (true) {
                System.out.println("等待客户端连接");
                Socket socket = serverSocket.accept();
                System.out.println("一个客户已连接");

                ClientHandler handler = new ClientHandler(socket);
                threadPool.execute(handler);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        WebServer server = new WebServer();
        server.start();
    }
}
